package com.app.controller;

import com.app.common.Result;
import com.app.model.PopularScienceModel;
import com.app.model.StatuteModel;
import com.app.server.PopularScienceService;
import com.app.server.StatuteService;
import com.app.utils.CommonHelp;
import com.app.vo.PopularScienceVo;
import com.app.vo.StatuteVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 科教宣传
 */
@Slf4j
@RestController
@RequestMapping("/sbbz/statute")
public class StatuteController {

    @Autowired
    private StatuteService statuteService;

    // 查询列表
    @GetMapping("/getPopularList")
    public Result getPopularScienceList(@RequestParam(defaultValue = "0") Integer page,
                                        @RequestParam(defaultValue = "10") Integer limit,String keyword) {
        StatuteVo popularScienceList = statuteService.getPopularList(page, limit,keyword);
        return Result.success(popularScienceList);
    }

    //根据id查询
    @GetMapping("/getPopularById")
    public Result getPopularScienceById(Integer id) {
        StatuteModel popularScience = statuteService.getById(id);
        return Result.success(popularScience);
    }

    // 新增和修改
    @PostMapping("/addAndUpdatePopular")
    public Result addAndUpdatePopularScience(@RequestBody StatuteModel popularScienceModel) {
        StatuteModel popularScience = statuteService.getById(popularScienceModel.getId());
        if (popularScience != null) {
            popularScienceModel.setUpdateTime(CommonHelp.getNowTimestamp());
            statuteService.updateById(popularScienceModel);
            return Result.success("修改成功");
        } else {
            popularScienceModel.setCreateTime(CommonHelp.getNowTimestamp());
            statuteService.save(popularScienceModel);
            return Result.success("添加成功");
        }
    }

    //删除
    @GetMapping("/delPopular")
    public Result delPopularScience(Integer id) {
        statuteService.removeById(id);
        return Result.success("删除成功");
    }

}
